package com.cxm.report.controller;

import com.cxm.report.config.TaskConfig;
import com.cxm.report.model.IReport;
import com.cxm.report.model.criteria.ReportFilter;
import com.cxm.report.model.data.Report;
import com.cxm.report.model.data.ReportDetail;
import com.cxm.report.model.data.TrendReport;
import com.cxm.report.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @author scl.BlueOcean
 * @version 1.0
 * @date 2018年07月02 18:32
 * @description
 */
@RestController
@RequestMapping("report")
@Slf4j
public class ReportController {
    @Autowired
    private BrandReportService brandReportService;
    @Autowired
    private ChannelReportService channelReportService;
    @Autowired
    private EfficiencyReportService efficiencyReportService;
    @Autowired
    private ChannelBrandReportService channelBrandReportService;

    @RequestMapping("")
    public List<Report> report(@RequestBody ReportFilter filter){
        IReportService reportService = getService(filter);
        switch (filter.getScopeType()){
            case IReport.ScopeType.DAY:
                return reportService.dayReport(filter);
            case IReport.ScopeType.WEEK:
                return reportService.weekReport(filter);
            case IReport.ScopeType.MONTH:
            return reportService.monthReport(filter);
            case IReport.ScopeType.TIME_SCOPE:
                return reportService.timeScopeReport(filter);
            default:return null;

        }
    }
    @RequestMapping("trend")
    public Object trend(@RequestBody ReportFilter filter){
        IReport.Type type = IReport.Type.getByCode(filter.getType());
        TrendReport trendReport = new TrendReport();
        if (type== IReport.Type.BRAND || type== IReport.Type.CHANNEL){
            List<Report> reports = report(filter);
            Collections.reverse(reports);
            TrendReport.TrendData orderNum = trendReport.new TrendData("查询量");
            TrendReport.TrendData successNum = trendReport.new TrendData("查询成功");
            TrendReport.TrendData overTimeNum = trendReport.new TrendData("查询超时");
            TrendReport.TrendData noRecordNum = trendReport.new TrendData("查无记录");
            TrendReport.TrendData inquiryNum = trendReport.new TrendData("查询中");
            TrendReport.TrendData failedNum = trendReport.new TrendData("查询失败");

            trendReport.addData(orderNum).addData(successNum).addData(overTimeNum).addData(noRecordNum).addData(inquiryNum)
                    .addData(failedNum);

            for (Report report : reports) {
                ReportDetail total = (ReportDetail) report.getTotal();
                trendReport.getCategories().add(report.getDate());
                orderNum.getData().add(total.getOrderNum());
                successNum.getData().add(total.getSuccessNum());
                overTimeNum.getData().add(total.getOverTimeNum());
                noRecordNum.getData().add(total.getNoRecordNum());
                inquiryNum.getData().add(total.getInquiryNum());
                failedNum.getData().add(total.getFailedNum());

            }
        }
        return trendReport;
    }

    private IReportService getService(ReportFilter filter) {
        IReport.Type type = IReport.Type.getByCode(filter.getType());
        switch (type){
            case BRAND:
                return brandReportService;
            case CHANNEL:
                return channelReportService;
            case EFFICIENCY:
                return efficiencyReportService;
            case CHANNEL_BRAND:
                return channelBrandReportService;
            default:return null;
        }
    }

    @Autowired
    private TaskConfig taskConfig;
    @RequestMapping("send")
    public void sendMail(String type){
        switch (type){
            case "1":taskConfig.dayReport();break;
            case "2": taskConfig.weekReport();break;
            default:
                taskConfig.dayReport();
                taskConfig.weekReport();
                break;
        }
    }
    @RequestMapping("refreshData")
    public void refreshData(String type,String data){
        switch (type){
            case "1":channelReportService.refreshData(data);break;
            case "2": brandReportService.refreshData(data);break;
            case "4": channelBrandReportService.refreshData(data);break;
            case "3": efficiencyReportService.refreshData(data);break;
            default:taskConfig.refreshData();
                break;
        }
    }
}
